# -*- coding: utf-8 -*-
import scrapy
from MtimeSpider.items import MtimespiderItem

class MtimePositionSpider(scrapy.Spider):
    name = 'mtime_position'
    allowed_domains = ['mtime.com']
    start_urls = ['http://www.mtime.com/top/movie/top100/']

    def parse(self, response):
        #循环获取节点内容
        for node in response.xpath('//div[@class="top_list"]/ul/li'):
            item = MtimespiderItem()
            #获取信息相关节点
            mov_con = node.xpath('./div[@class="mov_con"]')
            item['movie_name'] = mov_con.xpath('./h2/a/text()').extract_first()
            item['movie_url'] = mov_con.xpath('./h2/a/@href').extract_first()
            #获取导演和演员信息
            actors = mov_con.xpath('./p/a/text()').extract()
            if actors and len(actors)>0:
                item['director'] = actors[0]
            if actors and len(actors)>1:
                item['actor'] = actors[1]
            if actors and len(actors)>2:
                item['actor'] += '|'+actors[2]
            #获取类型信息
            types = mov_con.xpath('./p/span/a/text()').extract()
            if types and len(types) > 0:
                item['movie_type'] = types[0]
            if types and len(types) > 1:
                item['movie_type'] += '|'+types[1]
            #获取内容介绍
            item['introduce'] = mov_con.xpath('./p[@class="mt3"]/text()').extract_first()
            #获取评分
            points = node.xpath('./div[@class="mov_point"]/b/span/text()').extract()
            if points:
                item['point'] = points[0]+points[1]
            else:
                item['point'] = '0.0'
            yield  item
            
        #所有的分页url
        pages = ['1-10','11-20','21-30','31-40','41-50','51-60','61-70','71-80','81-90','91-100']
        urls = ['http://www.mtime.com/top/movie/top100/index-1.html',
            'http://www.mtime.com/top/movie/top100/index-2.html',
            'http://www.mtime.com/top/movie/top100/index-3.html',
            'http://www.mtime.com/top/movie/top100/index-4.html',
            'http://www.mtime.com/top/movie/top100/index-5.html',
            'http://www.mtime.com/top/movie/top100/index-6.html',
            'http://www.mtime.com/top/movie/top100/index-7.html',
            'http://www.mtime.com/top/movie/top100/index-8.html',
            'http://www.mtime.com/top/movie/top100/index-9.html',
            'http://www.mtime.com/top/movie/top100/index-10.html',
        ]
        # 获取分页数据
        now_page = response.xpath('//div[@id="PageNavigator"]/a[@class="on"]/text()').extract_first()
        #查看当前的分页获取下一页的链接
        next_page = ''
        for k,v in enumerate(pages):
            if v == now_page and k < len(pages)-1:
                next_page = urls[int(k)+1]
        if next_page:
            #重新发起下一次的采集指令
            yield scrapy.Request(next_page,callback=self.parse)